<?php
session_start();
require_once('../piphp/mysql.php');

$action=$_POST['action'];
$tbname=$_POST['tbname'];
$duty = $_SESSION['duty'];

$event =array();
$event['duty'] = $duty;
//如果存在事件
if($_POST['hasEvent']=='true')
{
	//定义与事件相关的变量,使之尤为一个数组
	$event['id'] = $_POST['id'];
	$event['start'] = $_POST['start'];
	$event['end'] = $_POST['end'];
	$event['title'] = $_POST['title'];
	$event['body'] = $_POST['body'];
}

//初始化变量
$response="";
$roomsql=new mysql();

switch($action)
{
	case 'update':
		$sql=update($tbname,$event);
		break;
	case 'delete':
		$sql=delete($tbname,$event);
		break;
	case 'insert':
		//获取要插入日期的前10个字符
		$datestr=substr($event['start'],0,10);
		//同一天内不能插入同一部门两次会议
		$sql = "SELECT `startTime` FROM `room_$tbname` WHERE `duty`=$duty;";
		$result=$roomsql->query($sql);
		while($arr=$roomsql->fetch_array($result))
		{
			//正则匹配
			if(preg_match('/'.$datestr.'/',$arr[0]))
			{
				//说明已经有同一个事情了，插入不成功
				exit("未能创建成功<br />注意：同一部门一天至多一次预约！");
			}
		}
		$sql=insert($tbname,$event);
		break;
	case 'load':
		$sql=load($tbname);
		break;
	case 'deleteall':	
		$sql=deleteall($tbname);
		break;
	case 'dropTable':
		$sql=dropTable($tbname);
		break;
	case 'maxID':
		$sql=maxID($tbname);
		break;
	case 'createTable':
		$sql=createTable($tbname);
		break;
	
	case 'dutyID':
		exit($_SESSION['duty']);	
	case 'showTables':
		$sql="SHOW TABLES;";
		break;
	default:
		$response="操作未成功";	
}

try{	
	if($action=='load')
	{
		//返回数据集
		$result=$roomsql->query($sql);
		//创建二维数组
		$rpsJSON=array();
		//提取指定表中的数据
		while($arr=$roomsql->fetch_array($result))
		{
			$temp=array(
				"id"	=>	$arr['id'],
				"start"	=>	$arr['startTime'],
				"end"	=>	$arr['endTime'],
				"title"	=>	$arr['title'],
				"body"	=>	$arr['content'],
				"duty"	=>	$arr['duty']
			);
			
			//向二维数组添加元素
			array_push($rpsJSON,$temp);
		}
		header('Content-Type:text/json');
		echo json_encode($rpsJSON);
		
	}
	else if($action=='maxID')
	{
		$result=$roomsql->query($sql);
		if($result==null)
			echo "0";
		else
		{
			$arr=$roomsql->fetch_array($result);
			echo $arr[0];
		}
	}
	else if($action=='showTables')
	{
		$result=$roomsql->query($sql);
		$rpsJSON=array();
		if($result==null)
		{
			echo 0;
		}
		else
		{
			while($arr=$roomsql->fetch_array($result))
			{
				//只处理以room_开关的表
				if(preg_match('/room_/',$arr[0]))
				{
					//先将前缀room_这5个字符去掉
					$str=substr($arr[0],5);
					//向数组中添加元素
					array_push($rpsJSON,$str);
				}
			}
			echo json_encode($rpsJSON);
		}
	}
	else
	{
		$roomsql->query($sql);
		echo "操作成功，已经反映到数据库";
	}
	
}
catch(Exception $e)
{
	//数据库错误显示
	echo $response.'<br/>';
	$roomsql->errormsg($e);
}

function update($tbname,$event)
{
	$id = $event['id'];
	$start = $event['start'];
	$end = $event['end'];
	$title = $event['title'];
	$body = $event['body'];
	//如果相应的id是存在,则只更新
	$sql="UPDATE `room_$tbname` SET `startTime`='$start',`endTime`='$end',`title`='$title',`content`='$body' WHERE `id`=$id;";
	return $sql;
}

function delete($tbname,$event)
{
	$id = $event['id'];
	$sql="DELETE FROM `room_$tbname` WHERE `id`=$id;";
	return $sql;
}

function deleteall($tbname)
{
	$sql="TRUNCATE `room_$tbname`";
	return $sql;
}

function dropTable($tbname)
{
	$sql = "DROP TABLE IF EXISTS `room_$tbname`";
	return $sql;
}

function insert($tbname,$event)
{
	$id = $event['id'];
	$start = $event['start'];
	$end = $event['end'];
	$title = $event['title'];
	$body = $event['body'];
	$duty = $event['duty'];
	$sql="INSERT INTO `room_$tbname`(`id`, `startTime`, `endTime`, `title`, `content`, `duty`) VALUES
			($id,'$start','$end','$title','$body',$duty);";
	return $sql;
}

function load($tbname)
{
	$sql="SELECT * FROM `room_$tbname`";
	return $sql;
}

function maxID($tbname)
{
	$sql="SELECT MAX( id ) FROM `room_$tbname` ";
	return $sql;
}

function createTable($tbname)
{
	$sql="CREATE TABLE IF NOT EXISTS `room_$tbname` (
			  `id` int(10) unsigned NOT NULL,
			  `startTime` varchar(50) NOT NULL,
			  `endTime` varchar(50) NOT NULL,
			  `title` varchar(255) NOT NULL,
			  `content` varchar(1000) DEFAULT NULL,
			  `duty` int(11) NOT NULL,
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	return $sql;
}

?>